package com.cskaoyan.user.controller;




import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dto.LoginGetDto;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.dto.KaptchaCodeResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.service.IUserRegisterService;
import com.netflix.client.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class CaptchaController {

    @Autowired
    IKaptchaService kaptchaService;
    @Autowired
    ILoginService loginService;

    /**
     * 获取验证码
     */
    @GetMapping("/kaptcha")
    public ResponseData getKaptchaCode(HttpServletResponse response) {
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.getKaptchaCode();
        Cookie cookie = CookieUtil.genCookie("kaptcha_uuid", kaptchaCodeResponse.getUuid(), "/", 60);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return new ResponseUtil<>().setData(kaptchaCodeResponse.getImageCode());

    }

    @PostMapping("/kaptcha")
    public ResponseData validKaptchaCode(@NotNull @RequestBody String code, HttpServletRequest httpServletRequest) {
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(code);
        kaptchaService.validateKaptchaCode(request);
        return new ResponseUtil<>().setData(null);


    }

    @Autowired
    IUserRegisterService iUserRegisterService;

    @PostMapping("/register")
    public ResponseData register(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest httpServletRequest){

        //验证验证码的正确性
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(userRegisterRequest.getCaptcha());
        kaptchaService.validateKaptchaCode(request);

        //向数据库中插入数据
        iUserRegisterService.register(userRegisterRequest);
        return new ResponseUtil<>().setData(null,"success");
    }

    @GetMapping("/verify")
    public ResponseData verify(UserVerifyRequest userVerifyRequest){

        //激活
        iUserRegisterService.verify(userVerifyRequest);
        return new ResponseUtil<>().setData(null,"success");

    }

    @PostMapping("/login")
    public ResponseData login(@RequestBody UserLoginRequest userLoginRequest ,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){

        //验证验证码的正确性
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        @NotEmpty String captcha = userLoginRequest.getCaptcha();
        request.setCode(captcha);
        kaptchaService.validateKaptchaCode(request);

        //登录
        UserLoginResponse loginResponse = loginService.login(userLoginRequest,httpServletResponse);

        return new ResponseUtil<>().setData(loginResponse, "success");
    }

    @GetMapping("login")
    public ResponseData login(HttpServletRequest httpServletRequest){
        String userInfo = httpServletRequest.getHeader("user_info");
        HashMap hashMap = JSON.parseObject(userInfo, HashMap.class);

        LoginGetDto loginGetDto = new LoginGetDto();
        loginGetDto.setUid((Integer) hashMap.get("uid"));
        loginGetDto.setFile((String) hashMap.get("file"));

        return new ResponseUtil<>().setData(loginGetDto, "success");
    }

    @GetMapping("/loginOut")
    public ResponseData logout(HttpServletRequest request , HttpServletResponse response){

        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if("access_token".equals(cookie.getName())){
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
        }
        return new ResponseUtil<>().setData(null,"success");

    }

}
